package com.meiyu.live.ui.main.widget;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.meiyu.live.R;
import com.meiyu.live.popj.Column;
import com.meiyu.live.ui.main.ColumnDetailActivity;

public class ColumnItemView extends FrameLayout implements View.OnClickListener {

    private static final int MAX_ITEMS = 6;

    private TextView tv_title;
    private View layout_more;
    private LinearLayout layout_hlist;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    private Column bean;

    public ColumnItemView(Context context) {
        super(context);
        init(context);
    }

    public ColumnItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ColumnItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.image_loading)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.image_loading)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.image_loading)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new FadeInBitmapDisplayer(200))// 图片加载好后渐入的动画时间
                .build();
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.adapter_column, this);
        tv_title = (TextView) findViewById(R.id.tv_title);
        layout_more = findViewById(R.id.layout_more);
        layout_more.setOnClickListener(this);
        layout_hlist = (LinearLayout) findViewById(R.id.layout_hlist);

        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int itemWidth = dm.widthPixels / 3;
        for (int i = 0; i < MAX_ITEMS; i++) {
            ColumnDetailItemView itemView = new ColumnDetailItemView(context);
            itemView.setImageLoader(imageLoader, options);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
            layout_hlist.addView(itemView, params);
        }
    }

    public void setData(Column bean) {
        this.bean = bean;
        tv_title.setText(bean.getAssort_name());
        for (int i = 0; i < MAX_ITEMS || i < bean.getLive_topN().size(); i++) {
            if (i < MAX_ITEMS && i < bean.getLive_topN().size()) {
                ColumnDetailItemView itemView = (ColumnDetailItemView) layout_hlist.getChildAt(i);
                itemView.setVisibility(View.VISIBLE);
                itemView.setData(bean.getLive_topN().get(i));
            } else if (i < MAX_ITEMS) {
                layout_hlist.getChildAt(i).setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View view) {
        if(view == layout_more){
            if(bean!=null && !TextUtils.isEmpty(bean.getAssort_name())){
                Intent intent = new Intent(getContext(), ColumnDetailActivity.class);
                intent.putExtra(ColumnDetailActivity.COLUMN_NAME,bean.getAssort_name());
                intent.putExtra(ColumnDetailActivity.COLUMN_ID,bean.getAssort_id());
                getContext().startActivity(intent);
            }
        }
    }
}
